import SQLiteStorage from 'react-native-sqlite-storage';
import SQLiteHelper from '../SQLiteHelper.js';

export default class ChapterInfo {
    constructor() {
        this.chapter_table_name = 'chapter_info_table';
        this.Hepler = new SQLiteHelper();
        this.db = null;
    }

    async createChapterInfoTable() {
        return new Promise((resolve, reject) => {
            if (!this.db) {
                this.Hepler.openDatabase().then(() => { console.log('okCourseInfo'); }, (err) => {
                    console.log(err);
                });
                this.db = this.Hepler.db;
            }
            if (!this.db) {
                reject('open db failed');
            }
            this.db.transaction((tx) => {
                tx.executeSql('CREATE TABLE IF NOT EXISTS ' + this.chapter_table_name + '(' +
                    'id INTEGER PRIMARY KEY NOT NULL,' +
                    'courseid VARCHAR,' +
                    'chaptervalue TEXT' +
                    ');'
                    , [], () => {
                        resolve();
                    }, (err) => {
                        reject(err);
                    });
            });

        });
    }

    async insertChapterInfo(courseid, chaptervalue) {
        return new Promise((resolve, reject) => {
            if (!this.db) {
                this.Hepler.openDatabase().then(() => { console.log('ok'); }, (err) => {
                    console.log(err);
                });
                this.db = this.Hepler.db;
            }
            if (!this.db) {
                reject('open db failed');
            }
            if (this.db) {
                this.db.executeSql(
                    'INSERT INTO ' + this.chapter_table_name + ' (courseid,chaptervalue) VALUES(?,?)',
                    [courseid, chaptervalue],
                    () => {
                        resolve();
                    },
                    (err) => {
                        reject(err);
                    });
            } else {
                reject('db not open');
            }
        });
    }
    async queryChapterInfo(courseid) {
        return new Promise((resolve, reject) => {
            if (!this.db) {
                this.Hepler.openDatabase().then(() => { console.log('ok'); }, (err) => {
                    console.log(err);
                });
                this.db = this.Hepler.db;
            }
            if (!this.db) {
                reject('open db failed');
            }
            if (this.db) {
                this.db.executeSql('SELECT * FROM ' + this.chapter_table_name + ' where courseid = ?', [courseid],
                    (results) => {
                        if (results.rows.length > 0) {
                            resolve(results.rows);
                        } else {
                            resolve(null);
                        }
                    }, (err) => {
                        reject(err);
                    });
            } else {
                reject('db not open');
            }
        });
    }
    async queryAllChapterInfo() {
        return new Promise((resolve, reject) => {
            if (!this.db) {
                this.Hepler.openDatabase().then(() => { console.log('ok'); }, (err) => {
                    console.log(err);
                });
                this.db = this.Hepler.db;
            }
            if (!this.db) {
                reject('open db failed');
            }
            if (this.db) {
                this.db.executeSql('SELECT * FROM ' + this.chapter_table_name, [],
                    (results) => {
                        if (results.rows.length > 0) {
                            resolve(results.rows);
                        } else {
                            resolve(null);
                        }
                    }, (err) => {
                        reject(err);
                    });
            } else {
                reject('db not open');
            }
        });
    }

    async dropChapterInfo() {
        return new Promise((resolve, reject) => {
            if (!this.db) {
                this.Hepler.openDatabase().then(() => { console.log('ok'); }, (err) => {
                    console.log(err);
                });
                this.db = this.Hepler.db;
            }
            if (!this.db) {
                reject('open db failed');
            }//DROP TABLE IF EXISTS tbl_name;
            if (this.db) {
                this.db.executeSql('DROP TABLE IF EXISTS ' + this.chapter_table_name, [],
                    (results) => {
                        if (results) {
                            resolve();
                        } else {
                            reject('delete fail');
                        }
                    }, (err) => {
                        reject(err);
                    });
            } else {
                reject('db not open');
            }
        });
    }

    async closeChapterInfo() {
        return new Promise((resolve, reject) => {
            if (this.db) {
                console.log('close ChapterInfo ok');
                this.Hepler.closeDatabase();
                resolve();
            }
            if (this.db) {
                reject('close db failed');
            }
        });
    }

}